package com.atguigu.mapreduce.solution;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.util.Tool;

public class SolutionDriver implements Tool {
    private Configuration conf;

    @Override
    public int run(String[] args) throws Exception {
        Job job1 = Job.getInstance(conf);
        job1.setMapperClass(SolutionMapper1.class);
        job1.setReducerClass(SolutionReducer1.class);
        job1.setCombinerClass(SolutionReducer1.class);
        job1.setMapOutputKeyClass(Text.class);
        job1.setMapOutputValueClass(IntWritable.class);
        FileInputFormat.setInputPaths(job1, new Path("input"));
        FileOutputFormat.setOutputPath(job1, new Path("output/solution_temp"));

        Job job2 = Job.getInstance(conf);
        job2.setMapperClass(SolutionMapper2.class);
        job2.setReducerClass(SolutionReducer2.class);
        job2.setMapOutputKeyClass(Text.class);
        job2.setMapOutputValueClass(Text.class);
        FileInputFormat.setInputPaths(job2, new Path("output/solution_temp"));
        FileOutputFormat.setOutputPath(job2, new Path("output/solution"));

        job1.setOutputKeyClass(Text.class);
        job1.setOutputValueClass(IntWritable.class);
        job1.setOutputFormatClass(SequenceFileOutputFormat.class);
        job2.setInputFormatClass(SequenceFileInputFormat.class);

        return job1.waitForCompletion(true) && job2.waitForCompletion(true) ? 0 : 1;
    }

    @Override
    public void setConf(Configuration configuration) {
        this.conf = configuration;
    }

    @Override
    public Configuration getConf() {
        return conf;
    }
}
